<?php
session_start() ;
{$_SESSION[ "service_dir" ]  = str_replace("\\","/",getcwd()) ;} ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "ByteBuffer.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "DataUtil.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "Session.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "Result.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "UtilHash.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "UtilKey.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "phpmailer/class.phpmailer.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "phpmailer/class.smtp.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "PWM/StrUtility.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "DataApp.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "DataAppManager.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "DataArea.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "DataAreasManager.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "DataFbToken.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "DataFriends.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "DataGroup.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "DataGroupUsers.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "DataGroupsManager.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "DataGuest.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "DataUserGroups.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "DataUserCertificates.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "DataUserRegister.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "DataWaitingConfirm.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "FunctionApp.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "FunctionArea.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "FunctionData.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "FunctionGroup.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "FunctionSession.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "FunctionTransaction.php"  )  ;
function readPattern($file_name){
	$file = fopen($file_name,r);
	$line=false;
	if($file){
		$line = fgets($file);
		while (!feof($file))$line .= fgets($file);
	}
	{ $ptn  = new Util\str\Pattern( $line );} ;
	return $ptn;
}
function publishResult($rs){
	$str = "";
	if(is_array($rs->data) && count($rs->data)>0){
		$ptn_message= readPattern($_SESSION[ "service_dir" ]  .'/pattern/' .'ptn_'. "section_error" .'.php' );
		$ptn_message_item = readPattern($_SESSION[ "service_dir" ]  .'/pattern/' .'ptn_'. "item_error" .'.php' );
		foreach($rs->data as $data){
			$ptn_message_item->message = $data;
			$ptn_message->items_error .=  $ptn_message_item ->publish() ;
		}
		$str .=  $ptn_message ->publish() ;
	}	
	return $str;
}
function addSystemArea($areaname){
	$amng = FunctionData::readAreasManager();
	if(!FunctionData::isSystemAreaExists($areaname)){
		$amng->addSystemArea($areaname);
		FunctionData::writeAreaManager($amng);
		$aobj = new DataArea();
		$aobj->setDescription("test");
		{if(!isset( FunctionData::$static  ) ||  FunctionData::$static  ==null){ FunctionData::$static   = new Memcache; FunctionData::$static  ->connect("127.0.0.1" , 11215 ) or die ("Could not connect");};} ;
		{if( FunctionData::$static  ->get( "area_system_" .$areaname )==null) FunctionData::$static  ->set( "area_system_" .$areaname , $aobj->getBinary() );else  FunctionData::$static  ->replace( "area_system_" .$areaname , $aobj->getBinary() );} ;
	}else{
		{Error::getInstance() ->addError( 7  , "Exists name" );} ;
	}
}
function addSystemGroup($groupname){
	$amng = FunctionData::readGroupsManager();
	if(!FunctionData::isSystemGroupExists($groupname)){
		$amng->addSystemGroup($groupname);
		FunctionData::writeGroupsManager($amng);
		$aobj = new DataGroup();
		FunctionData::writeGroup($groupname,$aobj);
		$uobj = new DataGroupUsers();
		FunctionData::writeGroupUsers($groupname,$uobj);
	}else{
		{Error::getInstance() ->addError( 9  , "Exists name" );} ;
	}
}
function addUserToGroup($user_email,$group_name){
	if(FunctionData::checkEmailRegisted($user_email) && FunctionData::isSystemGroupExists($group_name)){
		if($users = FunctionData::readGroupUsers($group_name)){
			$users->addUser($user_email);
			FunctionData::writeGroupUsers($group_name,$users);
			$join_groups = FunctionData::readUserGroups($user_email);
			$join_groups->addGroup($group_name);
			FunctionData::writeUserGroups($user_email,$join_groups);
		}else{
			{Error::getInstance() ->addError( 12  , "Unknown Error Please contact Administrator!" );} ;
		}
	}else{
		{Error::getInstance() ->addError( 11  , "Invalid Email or Group not exists!" );} ;
	}
}
function setLicenseToGroup($area,$role,$group_name){
	{Error::getInstance() ->addError( 12  , "readgroup ".$group_name );} ;
	$group = FunctionData::readGroup($group_name);
	{Error::getInstance() ->addError( 12  , "finish read group" );} ;
	if($group!=null){
		$group->addRole($area,$role);
		FunctionData::writeGroup($group_name,$group);
	}else{
		{Error::getInstance() ->addError( 13  , "Group Not Exists!" );} ;
	}
}
var_dump($_REQUEST);
$step = 0 + $_REQUEST["step"];
$working_str = '';
if($step == null){
	$working_str = '
	<form method="post">
		<input type="hidden" name="step" value = "1"/>
		<div>Email Address</div>
		<input name="email_address" value=""/>
		<div>Password </div>
		<input name ="password" type="password"/>
		<div>Retype Password</div>
		<input name="pass_confirm" type="password"/>
		<input type="submit" value="Install"/>
	</form>';
}else if($step == 1){
	$root_email = $_REQUEST["email_address"];
	$root_password = $_REQUEST["password"];
	$root_password_confirm = $_REQUEST["pass_confirm"];
	if(strlen($root_email) > 0 && strlen($root_password) > 0 && $root_password == $root_password_confirm){
		echo "register<br/>";
		$rs = FunctionTransaction::registerAccount($root_email,$root_password);
		$working_str = publishResult($rs);
		if($rs->return){
			FunctionTransaction::confirmRegistration($root_email,$rs->data["code"]);
			addSystemArea("AreaRoot" );
			addSystemArea("AccountManagement" );
			addSystemArea("ApplicationManagement" );
			{Error::getInstance() ->addError( 9  , "add group" );} ;
			addSystemGroup("Administrator" );
			{Error::getInstance() ->addError( 9  , "test1" );} ;
			addUserToGroup($root_email,"Administrator" );
			{Error::getInstance() ->addError( 9  , "test2" );} ;
			setLicenseToGroup("AreaRoot" ,(9) ,"Administrator" );
			{Error::getInstance() ->addError( 9  , "test3" );} ;
			setLicenseToGroup("AccountManagement" ,(9) ,"Administrator" );
			{Error::getInstance() ->addError( 9  , "test4" );} ;
			setLicenseToGroup("ApplicationManagement" ,(9) ,"Administrator" );
			{Error::getInstance() ->addError( 9  , "test5" );} ;
			$working_str .= "success!";
		}else{
			$working_str .= "not success";
		}
	}
}else if($step == 3){
}else{
	$working_str = "unknown error";
}
{ $error  = Error::getInstance() ->error;} ;
$str = "";
if(is_array($error) && count($error)>0){
	$ptn_error = readPattern($_SESSION[ "service_dir" ]  .'/pattern/' .'ptn_'. "section_error" .'.php' );
	$ptn_error_item = readPattern($_SESSION[ "service_dir" ]  .'/pattern/' .'ptn_'. "item_error" .'.php' );
	$ptn_error->items_error = '';
	foreach($error as $error_item){
		$ptn_error_item->code = $error_item['code'];
		$ptn_error_item->message = $error_item['message'];
		$ptn_error->items_error .=  $ptn_error_item ->publish() ;
	}
	$str .=  $ptn_error ->publish() ;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
	<link rel="stylesheet" type="text/css" href="layout.css"/>
	<script type = "text/javascript" src="Tcommonzip.js"></script>
</head>
<body>
	<div style="width:300px;">
	<?php echo $working_str.$str;?>
	</div>
</body>
</html>